﻿var CentreCostPage = function(pageContainer) {

    CereriConcediiPage.superclass.constructor.call(this, pageContainer);

    var self = this;

    this.CanGoBack = false;

    var Elements = {
        centreList: pageContainer.find('.centreList')
    };

    var searchResultsInfo = null;
    var searchBar = null;
    var currentIndex = 0;
    var initialPageSize = 50;
    var pageSize = 20;

    var isLoading = false;
    var visibleElements = 0;
    var centreCost = new Array();

    var currentCallback = null;

    var Templates = {
        centruCostTemplate: pageContainer.find(".centruCostTemplate"),
        centruCostTemplateCompiled: null
    };

    self.initialize = override(self.initialize, function() {
        addSearchBar();
        addResultsInfo(Elements.centreList);
        app.translatePage(app.Pages.CentreCost);
        pageContainer.bind('onPageHide', function() {
            currentCallback = null;
        });

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(true);
        });
    });

    var addResultsInfo = function(element) {
        element.before($("#searchResultsInfoTemplate").html());
        var searchResultsInfoContainer = pageContainer.find(".search-results-container");
        searchResultsInfo = new SearchResultsInfo(searchResultsInfoContainer);
        searchResultsInfo.hide();
    };

    var addSearchBar = function() {
        pageContainer.find('.content').prepend($("#searchBarTemplate").html());
        var searchBarContainer = pageContainer.find(".searchBar:first");
        searchBar = new SearchBar(searchBarContainer);
        searchBar.initialize();

        searchBarContainer
            .bind('getData', function(e) {
                if (e.Data.reset)
                    resetList();
                self.getPullUpData();
            })
            .bind('cancel', function(e) {
                resetList();
                self.getPullUpData();
            });
    };

    this.refresh = function() {
        resetList();
        self.getPullUpData();
    };

    var resetList = function() {
        currentIndex = 0;
        isLoading = false;
        Elements.centreList.empty();
        centreCost = new Array();
    };

    this.setCallback = function(callback) {
        currentCallback = callback;
    };

    this.getPullUpData = function(callback) {
        if (isLoading)
            return;
        isLoading = true;
        var searchText = searchBar.getSearchText();
        var noItemsToLoad = currentIndex ? pageSize : initialPageSize;

        app.TMMService.TMM.GetCentreCost(searchText, currentIndex, noItemsToLoad, function(response) {
            if (currentIndex != response.Ref)
                return;
            if (response.Exception)
                return;

            addToList(response.FinalResult);

            if (!visibleElements)
                calculateVisibleElements();

            if (!response.FinalResult.length || (currentIndex == 0 && response.FinalResult.length < visibleElements))
                pageContainer.find(".loadMore").hide();
            else if (!currentIndex)
                pageContainer.find(".loadMore").show();

            currentIndex += response.FinalResult.length;
            isLoading = false;
        });
    };

    var calculateVisibleElements = function() {
        visibleElements = Math.ceil($(window).height() / Elements.centreList.find("li:first").outerHeight());
    };

    var addToList = function(data) {
        if (!data)
            return;

        if (!Templates.centruCostTemplateCompiled) {
            var source = Templates.centruCostTemplate.html();
            Templates.centruCostTemplateCompiled = Handlebars.compile(source);
        }

        var html = "";
        for (var i = 0; i < data.length; i++) {
            var c = data[i];
            centreCost.push(c);
            html += Templates.centruCostTemplateCompiled(c);
        }

        Elements.centreList.append(html);
        setActionsOnCentruCost();
    };
    var setActionsOnCentruCost = function() {
        Elements.centreList.find('li').unbind().tap(function() {
            if (!currentCallback)
                return;

            var index = Elements.centreList.children().index($(this));
            var cc = centreCost[index];
            currentCallback(cc);
        });
    };

    self.initialize();
};
extend(CentreCostPage, TMMobilePage);